/**
 * Created by zwsvane on 2017/9/11.
 */

//模拟下拉框多选
selectOptionAll(".selectOptionAll2");


//数据查询
function arrList(arr){



    var Jan ='<td class="m1" colspan="3">暂无数据</td>';
    var Feb ='<td class="m2" colspan="3">暂无数据</td>';
    var Mar ='<td class="m3" colspan="3">暂无数据</td>';
    var Apr ='<td class="m4" colspan="3">暂无数据</td>';
    var May ='<td class="m5" colspan="3">暂无数据</td>';
    var Jun ='<td class="m6" colspan="3">暂无数据</td>';
    var Jul ='<td class="m7" colspan="3">暂无数据</td>';
    var Aug ='<td class="m8" colspan="3">暂无数据</td>';
    var Sept ='<td class="m9" colspan="3">暂无数据</td>';
    var Oct ='<td class="m10" colspan="3">暂无数据</td>';
    var Nov ='<td class="m11" colspan="3">暂无数据</td>';
    var Dec ='<td class="m12" colspan="3">暂无数据</td>';

    var trCont ='';
    var mouthList ='';
    for(var i =0;i<arr.length;i++){
        var temp = arr[i].siteMonth.substr(5,2);
        var className = temp;
        className = className>9?className:className.substr(1,2);
        trCont ='<td class="m'+className+'">'+arr[i].orderEfficientNum+'</td><td class="m'+className+'">'+arr[i].workerNum+'</td><td class="m'+className+'">'+arr[i].siteEfficiency+'</td>';

        mouthList =mouthList+temp+',';

        if(temp=='01'){
            Jan = trCont;
        }
        else if(temp=='02'){
            Feb = trCont;
        }
        else if(temp=='03'){
            Mar = trCont;

        }
        else if(temp=='04'){
            Apr = trCont;

        }
        else if(temp=='05'){
            May = trCont;

        }
        else if(temp=='06'){
            Jun = trCont;
        }
        else if(temp=='07'){
            Jul = trCont;

        }
        else if(temp=='08'){
            Aug = trCont;
        }
        else if(temp=='09'){
            Sept = trCont;

        }
        else if(temp=='10'){
            Oct = trCont;
        }
        else if(temp=='11'){
            Nov = trCont;

        }
        else if(temp=='12'){
            Dec = trCont;
        };

    };

    $("<tr class='text-c'>"+Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sept+Oct+Nov+Dec+"</td></tr>").appendTo('#Jan');
    //console.log($(".cityAreaShow").attr("data-status"));
    if(($(".cityAreaShow").attr("data-status")!='')){
        var numList = $(".cityAreaShow").attr("data-status").split(',');
        $("#Jan tr").find("td").hide();
        $("#headerList th").hide();
        $(".systemLeft").css("width",200*numList.length+"px");

        for(var i =0;i<numList.length;i++){
            numList[i]=numList[i]>9?numList[i]:numList[i].substr(1,2);
            $("#Jan").find(".m"+numList[i]).show();
            $("#headerList").find(".m"+numList[i]).show();
        }

    }else {
        $("#Jan tr").find("td").show();
        $("#headerList th").show();
        $(".systemLeft").css("width","2400px");
    }

}


localStorage.nn=0;
var pageClick = false;
function ylhzList(provinceId,cityId,siteId,monthDate,keyWords,num){

    $.ajax({
        'type':'post',
        'dataType':'json',
        'url':WEBURL+'/summary/listall',
        'data':{
        'provinceId':provinceId,
        'cityId':cityId,
        'siteId':siteId,
        'siteMonths':monthDate,
        'keyWords':keyWords,
            'pageNo':num,
            'pageSize':10
        },
        success:function(r){
            if(r.code==0000){
                var ylhzSiteList ='';
                var ylhzSiteListdata ='';
               // console.log(r.data);
                $("#ylhz-month-site").html('');
                $("#Jan").html('');
                if(r.data.dataList.length>0){

                    $.each(r.data.dataList,function(index,item){
                        $('<tr class="text-c"><td>'+item.cityName+'</td><td>'+item.provinceName+'</td><td>'+item.siteName+'</td></tr>').appendTo($("#ylhz-month-site"));
                        //数据插入
                        arrList(item.siteList);

                    });



                }
                else{
                    console.log($(".cityAreaShow").attr("data-status"));

                    if(($(".cityAreaShow").attr("data-status")!='')){
                        var numList = $(".cityAreaShow").attr("data-status").split(',');
                        $("#Jan tr").find("td").hide();
                        $("#headerList th").hide();
                        $(".systemLeft").css("width",200*numList.length+"px");

                        for(var i =0;i<numList.length;i++){
                            numList[i]=numList[i]>9?numList[i]:numList[i].substr(1,2);
                            $("#Jan").find(".m"+numList[i]).show();
                            $("#headerList").find(".m"+numList[i]).show();
                        }

                    }else {
                        $("#Jan tr").find("td").show();
                        $("#headerList th").show();
                        $(".systemLeft").css("width","2400px");
                    }

                    $('<tr class="text-c"><td colspan="4">暂无数据</td></tr>').appendTo($("#ylhz-month-site"));
                    // ylhzSiteList = '<tr class="text-c"><td colspan="4">暂无数据</td></tr>';
                    // ylhzSiteListdata = '<tr class="text-c"><td colspan="36">暂无数据</td></tr>';
                    $('<tr class="text-c"><td colspan="36">暂无数据</td></tr>').appendTo("#Jan")
                };
                //分页
                $("#kkpager").html('');
                var totaldata = r.data.totalRecord;
                var totalpages = Math.floor((totaldata-1)/10)+1;

                var totalPage = totaldata;
                var pages =  totalpages;
                //console.log(pages);
                var totalRecords = totaldata;
                var pageNo =  localStorage.nn;

                if(!pageNo) {
                    pageNo = 0;
                }
                //有些参数是可选的，比如lang，若不传有默认值
                kkpager.generPageHtml({
                    pno: pageNo,
                    //总页码
                    total: totalpages,
                    //总数据条数
                    totalRecords: totaldata,
                    mode: 'click', //默认值是link，可选link或者click
                    click: function(n) {
                        var zdyl_provinceList =$("#provinceList").find("option:selected").attr("data-orgCode");
                        var zdyl_cityCompany =$("#cityList").find("option:selected").attr("data-orgCode");
                        var zdyl_siteId =$("#siteList").find("option:selected").attr("data-orgCode");
                        var zdyl_beginTime =$(".ylhz_yeardata").val();
                        var zdyl_keyWords =$("#zdyl-keyWords").val();


                        // do something
                        //手动选中按钮
                        this.selectPage(n);
                        localStorage.nn = n;
                        //pagelist(localStorage.nn);
                        ylhzList(zdyl_provinceList,zdyl_cityCompany,zdyl_siteId,zdyl_beginTime,zdyl_keyWords,(localStorage.nn-1));

                        return false;
                    }

                },true);

            }
        },
        error:function(e){
            console.log(e.msg)
        }
    })

}



$(function(){
    //关键词查询判断 省份不可选
    $("#zdyl-keyWords").blur(function(){

        if($(this).val()==''){
            $("#provinceList").prop('disabled',false);
            $("#cityList").prop('disabled',false);
            $("#siteList").prop('disabled',false);
        }
        else{
            $("#provinceList").prop('disabled','true');
            $("#cityList").prop('disabled','true');
            $("#siteList").prop('disabled','true');
        }

    });
    //省份查询 关键字不可输入
    $("#provinceList").change(function(){
        $("#zdyl-keyWords").prop('disabled','true');
    });

    //查询条件重置
    $(".ylhz-reset-btn").on("click",function(){
        $("#provinceList option").eq(0).prop("selected",'selected');
        $("#cityList option").eq(0).prop("selected",'selected');
        $("#siteList option").eq(0).prop("selected",'selected');
        $("#zdyl-keyWords").prop("disabled",false);
        $("#zdyl-keyWords").val('');
        $("#provinceList").prop('disabled',false);
        $("#cityList").prop('disabled',false);
        $("#siteList").prop('disabled',false);
        $("#cityAreaList em").removeClass('bgcheck');
        $("#zdyl-flag").val('请选择');
        $(".cityAreaShow").attr('data-status',' ');
        $("#ylhz_year").val('');
    });


    //查询条件查询
    $(".ylhz-search-btn").on("click",function(){
        //获取年份
        var yjhz_year =$("#ylhz_year").val();
             yjhz_year = yjhz_year.substr(0,(yjhz_year.length-1));
        var  provinceId=$("#provinceList").find("option:selected").attr("data-orgCode");;
        var  cityId=$("#cityList").find("option:selected").attr("data-orgCode");
        var  siteId=$("#siteList").find("option:selected").attr("data-orgCode");
        var  monthDate=$(".cityAreaShow").attr('data-status');
        var  keyWords=$("#zdyl-keyWords").val();

        if(yjhz_year==''||yjhz_year==undefined||yjhz_year==null){

            $.Huimodalalert('请选择年份！',2000);
            return false;
        }
        if($(".allSelectShowInput").val()=='请选择'){
            $.Huimodalalert('请选择月份！',2000);
            return false;
        }

        if(monthDate==''){

            monthDate ='X';
            //console.log(monthDate);
         }

        var mouthLsit = monthDate.split(',');
        for(var i = 0;i<mouthLsit.length;i++){
            mouthLsit[i]=yjhz_year+'-'+mouthLsit[i];
        };

        var monthDateList = '';
        var searchList = '';

        for(var i =0;i<mouthLsit.length;i++){
            monthDateList = monthDateList+mouthLsit[i]+'*';
            searchList = searchList +mouthLsit[i]+'&';
        }

        monthDateList = monthDateList.substr(0,(monthDateList.length-1));
        searchList = searchList.substr(0,(searchList.length-1));
       // console.log(yjhz_year);


        //给导出赋值时间值
        $(".ylhz_yeardata").val(monthDateList);

        //调用查询接口
        localStorage.nn =0;
       ylhzList(provinceId,cityId,siteId,monthDateList,keyWords,0);



    })


    //数据导出
    $(".ylhz-export-btn").on("click",function(){
        //console.log($("#zdyl-keyWords").val());

        // if($("#zdyl-keyWords").val()==' '||$("#zdyl-keyWords").val()==undefined||$("#zdyl-keyWords").val()==null){
        //     $.Huimodalalert('请选择时间！',2000);
        //     return;
        // }
        if($("#ylhz_year").val()==''){
            $.Huimodalalert('请选择时间！',2000);
            return false;
        }
        var timeLoad =   window.setTimeout(function(){
            $.Huimodalalert('数据下载中,请稍后。。。。',2000);
        },200);
        var oFrm = document.getElementById('iframe');
        //$(".loadZws").show();
        oFrm.onload = oFrm.onreadystatechange = function() {

            if (this.readyState && this.readyState != 'complete')  return;
            else {
                window.clearTimeout(timeLoad);
                alert('未查询到数据！');
            }

        }
      //  setTimeout(function () {
           // $(".loadZws").hide();
      //  },2000)
        var url =WEBURL +'/summary/export?provinceId='+ $("#provinceList option:selected").attr('data-orgcode')+'&cityId='+$("#cityList  option:selected").attr('data-orgcode')+'&siteId='+$("#siteList option:selected").attr('data-orgcode')+'&siteMonths='+$("#ylhz-export-form input[name=siteMonths]").val()+'&keyWords='+$("#zdyl-keyWords").val();
        $("#iframe").attr("src",url);


    })

})